import store from "@/store"
import baseVal from '@/config/base.js'
// 区分开发环境和生产环境  接口地址
let baseUrl;
if (process.env.NODE_ENV === 'development') {
	baseUrl = baseVal.devUrl // 开发环境
} else {
	baseUrl = baseVal.baseUrl // 生产环境
}

export function https(config) {
	//设置请求头
	config.header = {
		'content-type': 'application/json;charset=UTF-8'
	};
	//检查缓存中有没有token
	var token = uni.getStorageSync('token');
	if (token != '') {
		config.header.token = token
	}
	config.url = baseUrl + config.url; // 请求地址
	let promise = new Promise(function(resolve, reject) {
		uni.request(config).then(res => {
			let data = res.data
			if(data) {
				resolve(data)
			}
			if (data.code == 0) {
				uni.hideLoading()
				uni.showToast({
					title: data.msg,
					icon: "none",
					duration: 1500
				})
			} else if(data.code == 401){
				store.commit('DEL_TOKEN')
				store.commit('DEL_ADMIN')
				store.commit('DEL_INFO')
				uni.showModal({
					title: '提示',
					content: '暂未登录',
					confirmText: "去登录",
					confirmColor: "#5A68FF",
					success: function (res) {
						if (res.confirm) {
							uni.switchTab({
								url: "/pages/wo/index"
							})
						}
					}
				})
			}
		}).catch(error => {
			// 失败处理
			reject(error)
		})
	})
	return promise
}